package com.purong.purong.adapter;

import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.purong.purong.R;
import com.purong.purong.bean.SearchValueBean;
import com.purong.purong.util.CommonUtil;
import com.purong.purong.widget.RadiusImageView;
import com.zhy.view.flowlayout.FlowLayout;
import com.zhy.view.flowlayout.TagAdapter;
import com.zhy.view.flowlayout.TagFlowLayout;

import java.util.List;

/**
 * Created by Administrator on 2016/8/29.
 */
public class CreditSearchMoreColorAdapter extends TagAdapter {
    private List<SearchValueBean> tagList;;
    private Context mContext;
    private TagFlowLayout mflowLayout;
    private OnSpecClickListener mListener;
    public CreditSearchMoreColorAdapter(Context context, TagFlowLayout flowLayout, List<SearchValueBean> list){
        super(list);
        this.tagList=list;
        this.mContext=context;
        this.mflowLayout=flowLayout;
    }
    public void setOnSpecClickListener(OnSpecClickListener listener){
        this.mListener=listener;
    }
    @Override
    public View getView(FlowLayout parent, final int position, Object o) {
        View view= LayoutInflater.from(mContext).inflate(R.layout.credit_search_more_color_tag,mflowLayout,false);
        RelativeLayout mainLay=(RelativeLayout)view.findViewById(R.id.main_lay);
        mainLay.setLayoutParams(new ViewGroup.MarginLayoutParams((CommonUtil.getScreenWidth(mContext)-CommonUtil.dip2px(mContext,10))/5,CommonUtil.dip2px(mContext,50)));
        RadiusImageView iv=(RadiusImageView) view.findViewById(R.id.search_tag);
        ImageView check=(ImageView)view.findViewById(R.id.check_img);
        RelativeLayout.LayoutParams lp1=new RelativeLayout.LayoutParams(CommonUtil.dip2px(mContext,26),CommonUtil.dip2px(mContext,26));
        lp1.addRule(RelativeLayout.CENTER_IN_PARENT);
        iv.setLayoutParams(lp1);
        RelativeLayout.LayoutParams lp2=new RelativeLayout.LayoutParams(CommonUtil.dip2px(mContext,36),CommonUtil.dip2px(mContext,36));
        lp2.addRule(RelativeLayout.CENTER_IN_PARENT);
        check.setLayoutParams(lp2);

        if (!tagList.get(position).getItem().contains("other")){
//
            String colorString =tagList.get(position).getItem().replaceAll("#","");
//
            if (colorString.equals("ffffff")||colorString.equals("FFFFFF")){
                iv.setBackgroundResource(R.drawable.gray_circle_border);
            }else {
                iv.setImageDrawable(new ColorDrawable(new java.math.BigInteger ("ff"+colorString, 16).intValue ()));
            }

        }else {
            iv.setImageResource(R.drawable.cart_img_color);
        }
        if (tagList.get(position).isCheckTmp()){
            check.setVisibility(View.VISIBLE);
        }else {
            check.setVisibility(View.INVISIBLE);
        }
        iv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                for (int i = 0; i <tagList.size() ; i++) {
                    if (tagList.get(i).getItem().equals(tagList.get(position).getItem())){
                        tagList.get(i).setCheckTmp(!tagList.get(i).isCheckTmp());

                    }else {
                        tagList.get(i).setCheckTmp(false);

                    }
                }
                notifyDataChanged();
            }
        });
        return view;
    }
    public interface OnSpecClickListener{
        void onCheck(int position);
    }
}
